<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + " ://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE HTML>
<jsp:include page="root_public.jsp"/>
<title>管理员列表</title>
</head>

<body>
<div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="javascript:;">首页</a>
        <a href="<%=path%>/rootList">管理员管理</a>
      </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i></a>
</div>
<div class="x-body">
    <div class="layui-row">
        <form class="layui-form layui-col-md12 x-so">
            <input type="text" name="username" placeholder="请输入管理员名" autocomplete="off" class="layui-input username">
            <a class="layui-btn search"><i class="layui-icon">&#xe615;</i>
            </a>
        </form>
    </div>
    <xblock>
        <button class="layui-btn" onclick="x_admin_show('添加管理员','<%=path%>/rootAdd',450,550)"><i class="layui-icon">
        </i>添加
        </button>
        <span class="x-right" style="line-height:40px">共有数据：<span class="totalNum"></span> 条</span>
    </xblock>
    <table class="layui-table">
        <thead>
        <tr>
            <th>姓名</th>
            <th>登录名</th>
            <th>手机</th>
            <th>邮箱</th>
            <th>角色</th>
            <th>加入时间</th>
            <th>操作</th>
        </thead>
        <tbody class="bodyt">
        </tbody>
    </table>
    <div id="demo1"></div>
</div>
<script>
    var total = 0, $bodyt = $('.bodyt');

    layui.use(['laypage', 'layer', 'form'], function () {

        var form = layui.form,
            laypage = layui.laypage;

        function countAllNum(search) {
            $bodyt.html("");
            // 计算总数
            $.ajax({
                type: "post",
                url: "<%=path%>/user/countAllNum",
                async: true,
                dataType: "json",
                data: {
                    "search": search,
                },
                success: function (data) {
                    $('.totalNum').text(data.data);
                    total = data.data;
                    //总页数大于页码总数
                    laypage.render({
                        elem: 'demo1'
                        , count: total //数据总数
                        , jump: function (obj) {
                            console.log(obj);
                            this.count = total;
                            var start = (obj.curr - 1) * obj.limit;
                            list(search, start, "");
                        }
                    });
                }
            });
        }

        countAllNum("");

        // 模糊搜索
        $('.search').click(function () {
            countAllNum($('.username').val());
            form.render();
        });
    });

    // 用户列表
    function list(search, start, size) {
        $.ajax({
            type: "post",
            url: "<%=path%>/user/findList",
            async: true,
            dataType: "json",
            data: {
                "search": search,
                "start": start,
                "size": size
            },
            success: function (data) {
                var datas = data.data.userList,
                    time = "",
                    role = "普通管理员";
                $bodyt.html("");
                for (var i = 0; i < datas.length; i++) {
                    var datass = datas[i];
                    if (datass.createDate != null) {
                        time = datass.createDate;
                    } else {
                        time = "未知";
                    }
                    if (datass.role != null) {
                        role = datass.role.name;
                    }
                    var $html = $('        <tr>\n' +
                        '            <td>' + datass.name + '</td>\n' +
                        '            <td>' + datass.no + '</td>\n' +
                        '            <td>' + datass.phone + '</td>\n' +
                        '            <td>' + datass.email + '</td>\n' +
                        '            <td>' + role + '</td>\n' +
                        '            <td>' + time + '</td>\n' +
                        '            <td class="td-manage">\n' +
                        '                <a title="编辑" onclick="x_admin_show(\'编辑\',\'<%=path%>/rootEdit?id=' + datass.id + '\',450,550)" href="javascript:;">\n' +
                        '                    <i class="layui-icon">&#xe642;</i>\n' +
                        '                </a>\n' +
                        '                <a title="删除" onclick="member_del(this)" href="javascript:;">\n' +
                        '                    <i class="layui-icon">&#xe640;</i>\n' +
                        '                </a>\n' +
                        '                <a style="display: none;position:absolute;" class="ID">' + datass.id +
                        '</a>\n' +
                        '            </td>\n' +
                        '        </tr>');
                    $bodyt.append($html);
                }
            }
        });
    }

    /*管理员-删除*/
    function member_del(obj) {
        layer.confirm('确认要删除吗？', function (index) {
            //发异步删除数据
            var $id = $(obj).siblings(".ID").text();
            $.ajax({
                type: "post",
                url: "<%=path%>/user/delete",
                async: true,
                dataType: "json",
                data: {
                    "id": $id
                },
                success: function (data) {
                    $(obj).parents("tr").remove();
                    layer.msg('已删除!', {icon: 1, time: 600});
                }
            });
        });
    }
</script>
</body>

</html>